package org.yxing.palpalgps.activity;

import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.domain.AsyncImageLoader;
import org.yxing.palpalgps.domain.UserInfo;
import org.yxing.palpalgps.http.AsyncRequest;
import org.yxing.palpalgps.oauth.OAuth;
import org.yxing.palpalgps.utils.Helper;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class NewRegisterListActivity extends ListActivity {
    
    private static final String TAG = NewRegisterListActivity.class.getSimpleName();
    
    private Adapter mAdapter;
    
    private int iid;
    private int sid;
    
    private AsyncImageLoader mAsyncImageLoader = new AsyncImageLoader();
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new Adapter(this);
        
        Bundle bundle = getIntent().getExtras();
        iid = bundle.getInt("iid");
        sid = bundle.getInt("sid");
        
        setListAdapter(mAdapter);
        mAdapter.fetchData();
    }
    
    private class Adapter extends BaseAdapter {
        
        private JSONArray data = new JSONArray();
        
        private Context mContext;
        
        public Adapter(Context context) {
            mContext = context;
        }

        public int getCount() {
            return data.length();
        }

        public JSONObject getItem(int position) {
            JSONObject item = null;
            try {
                item = data.getJSONObject(position);
            } catch (JSONException e) {
                Log.e(TAG, e.getMessage());
            }
            return item;
        }

        public long getItemId(int position) {
            long id = 0;
            try {
                id = getItem(position).getLong("uid");
            } catch (JSONException e) {
                Log.w(TAG, e.getMessage());
            }
            
            return id;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.new_register_list, null);
            ImageView faceView = (ImageView) convertView.findViewById(R.id.face);
            TextView nameView = (TextView) convertView.findViewById(R.id.name);
            TextView distance = (TextView) convertView.findViewById(R.id.distance);
            TextView genderView = (TextView) convertView.findViewById(R.id.gender);
            
            JSONObject json = getItem(position);
            try {
                //name
                nameView.setText(json.getString("name"));
                //gender
                String gender = json.getString("gender");
                genderView.setText((gender.equals("m")) ? getString(R.string.male) : getString(R.string.female));
                //face
                String face = json.getString(UserInfo.KEY_FACE);
                if (face != null && face.length() > 1) {
                    String url = Helper.getSmallPictureUrl(face);
                    mAsyncImageLoader.loadDrawable(url, faceView);
                }
                //distance
                int dis = json.getInt("distance");
                distance.setText(getString(R.string.distance) + ": " + dis + " m");
            } catch (JSONException e) {
                Log.w(TAG, e.getMessage());
            }
//            
            return convertView;
        }
        
        public void fetchData() {
            List<BasicNameValuePair> params = OAuth.createAuthParams();
            if (iid > 0)
                params.add(new BasicNameValuePair("iid", Integer.toString(iid)));
            else if (sid > 0) 
                params.add(new BasicNameValuePair("sid", Integer.toString(sid)));
            
            AsyncRequest ar = new AsyncRequest(NewRegisterListActivity.this);
            ar.fetch("list/newest", params, new AsyncRequest.FetchListener() {
                
                @Override
                public void onError(int code, String msg) {
                    
                }
                
                @Override
                public void onComplete(JSONObject response) {
                    try {
                        data = response.getJSONArray("result");
                        notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    
    
    public void onListItemClick(ListView l, View v, int position, long id) {
        //go to user profile activity
        Intent intent = new Intent(this, UserProfileActivity.class);
        intent.putExtra("uid", Long.toString(id));
        startActivity(intent);
    }
}
